<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ManagerParam
 *
 * @author jhonny
 */
class ManagerParam {
    protected $sfActions;
    public function __construct($sfActions) {
        $this->sfActions = $sfActions;        
    }
    /**
     * Verifica que el parametro que viene de la URL esta en la lista de parametros
     * @param type $params
     * @param type $param
     * @return boolean 
     */
    protected function containsParameter($params,$param){
        $encontrado=false;
        foreach ($params as $key=>$value){
            if($param==$key){
                $encontrado=true;
                break;
            }
        }
        if($encontrado){
            $this->logMessage("URL params contains $param",'info');
        }else{
            $this->logMessage("URL params NO contains $param",'info');
        }
        return $encontrado;
    }
    /**
     * agrea ala url el nuevo parametro
     * @param type $params
     * @param type $strParams
     * @param type $parameter
     * @return type string
     */
    protected function addUrlParameter($params,$strParams,$parameter){
        $totalCount = count($params);
        if($totalCount>0){
            $strParams.="&";
        }
        $strParams.=$parameter;
        return $strParams;
    }
    protected function removeUrlParameter($params,$parameter){
        return $this->generateStrParameters($params,$parameter);
    }
    protected function getValueFromUrlParam($params, $param){
        $valueOut = "";
        foreach ($params as $key => $value) {
            if($param==$key){
                $valueOut = $value;
                break;
            }
        }
        return $valueOut;
    }
    protected function generateStrParametersPage($params,$pagina){
        $strParams = $this->removeUrlParameter($params, UrlParameter::pagina);
        $parametro=UrlParameter::pagina."=".$pagina;
        return $this->addUrlParameter($params, $strParams, $parametro);
        
    }
    protected function generateStrParameters($params,$ignoreId=null){
        $strValue = "";
        $sizeParams = 0;
        $count = 0;
        $sizeParams = count($params);
        foreach ($params as $key => $value) {
            $count++;
            if($ignoreId==null || $key!=$ignoreId){
                $this->logMessage("generando params url  $key => $value",'info');
                $strValue.="$key=$value";

                if(($count<$sizeParams && $ignoreId==null)||
                   ($count<($sizeParams-1) && $ignoreId!=null)){
                    $strValue.="&";
                }
            }
        }
        $this->logMessage("url generanda $strValue",'info');
        return $strValue;
    }
    protected function generateArrayParameters(sfWebRequest $request){
        
        $params = array();
        $this->addElementUrlParams($request,$params,UrlParameter::pagina,"1");
        $this->addElementUrlParams($request,$params,UrlParameter::cantidadPorPagina,sfConfig::get('app_max_instituciones_on_filters'));
        if($request->isMethod('post')){
            $this->addElementPostParams($request,$params,UrlParameter::idPais);
            $this->addElementPostParams($request,$params,UrlParameter::idRegion);
            $this->addElementPostParams($request,$params,UrlParameter::pagina);
        }else{
            $this->addElementUrlParams($request,$params,UrlParameter::idPais);
            $this->addElementUrlParams($request,$params,UrlParameter::idRegion);
            $this->addElementUrlParams($request,$params,UrlParameter::idPartido);
            $this->addElementUrlParams($request,$params,UrlParameter::idLocalidad);
            $this->addElementUrlParams($request,$params,UrlParameter::idBarrio);
            $this->addElementUrlParams($request,$params,UrlParameter::nivel);
            $this->addElementUrlParams($request,$params,UrlParameter::idioma);
            $this->addElementUrlParams($request,$params,UrlParameter::tipo);
            $this->addElementUrlParams($request,$params,UrlParameter::ideologia);
            $this->addElementUrlParams($request,$params,UrlParameter::internacional);
            $this->addElementUrlParams($request,$params,UrlParameter::internado);
            $this->addElementUrlParams($request,$params,UrlParameter::jornada);
            $this->addElementUrlParams($request,$params,UrlParameter::residencia);
            $this->addElementUrlParams($request,$params,UrlParameter::sexo);       
        }
        
        return $params;
        
    }
    protected function addElementPostParams(sfWebRequest $request,&$params,$key){
        $value = null;
        $value = $request->getPostParameter($key);
        
        $this->logMessage("params form post $key => $value",'info');
        if(!is_null($value)){            
            $params[$key]=$value;
            $this->logMessage("params array post $params",'info');
        }

    }
    protected function addElementUrlParams(sfWebRequest $request,&$params,$key,$default=""){
        $value = null;
        $value = $request->getParameter($key,$default);
        $this->logMessage("params url  $key => $value",'info');
        if($value!=null){
            //$item = array($key=>$value);
            $params[$key]=$value;
            //array_push($params, $item);
            $this->logMessage("params array  $params",'info');
        }
    }
    protected function logMessage($mensaje,$level){
        $this->sfActions->logMessage($mensaje,$level);
    }
}

?>
